package com.task.common.utils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.task.common.core.redis.RedisCache;
import com.task.common.domain.info.PlanInfo;
import com.task.common.enums.DataFileTypeEnum;
import com.task.common.enums.WeekEnum;
import com.task.common.exception.ServiceException;
import com.task.common.utils.date.DateConstants;
import com.task.common.utils.date.DateUtil;

/**
 * @author 迪迦.
 * @date 2024/11/13 17:21
 */
public class SystemFunctionUtil {

  public final static String TABLE_PREFIX = "data_data_content_";
  public final static String TABLE_PREFIX_INFO = "data_data_content_info_";

  public final static String REFERER_TABLE_PREFIX = "data_referer_content_";
  public final static String REFERER_TABLE_PREFIX_INFO = "data_referer_content_info_";

  public final static String RELATION_MISSION_DATA_PREFIX = "relation_mission_data_";
  public final static String RELATION_MISSION_REFERER_PREFIX = "relation_mission_referer_";

  //数据发送详情记录表前缀
  public final static String HISTORY_DATA_GET_DETAIL = "history_data_get_detail_";

  //数据发送索引记录表前缀
  public final static String HISTORY_DATA_GET_INDEX = "history_data_get_index_";

  //任务发送详情记录表前缀
  public final static String HISTORY_MISSION_GET_DETAIL = "history_mission_get_detail_";

  /**
   * 根据id获取数据应该在那张表里面
   * @return 表名
   */
  public static String getTableNameById(String id, String dataType) {
    String tNum = id.substring(0, 1);
    if ("0".equals(tNum)) {
      tNum = "10";
    }
    String tablePre = DataFileTypeEnum.DATA.name().equals(dataType) ? TABLE_PREFIX : REFERER_TABLE_PREFIX;
    return tablePre + tNum;
  }

  public static String getDistributeTableNameById(String id, String dataType,Boolean relation) {
    long dataId = Long.parseLong(id);
    Long suf = (dataId % 20 + 1);
    String tablePre = "";
    if(relation){
      tablePre = DataFileTypeEnum.DATA.name().equals(dataType) ? RELATION_MISSION_DATA_PREFIX : RELATION_MISSION_REFERER_PREFIX;
    }else {
      tablePre = DataFileTypeEnum.DATA.name().equals(dataType) ? TABLE_PREFIX_INFO : REFERER_TABLE_PREFIX_INFO;
    }
    return tablePre + suf;
  }


  /**
   * 获取历史记录表按时间yyyyMM格式的表名
   * @return 表名
   */
  public static String getHistoryTableNameByTime(String dateFormat, String prefix) {
    return prefix + dateFormat;
  }

  /**
   * 获取历史记录表按时间yyyyMM格式的表名集合,用于查询任务关联数据发送历史,如果是月末最后一天有可能会出现任务历史在上一个月表中,数据历史出现在相邻两张表中
   * @return 表名
   */
  public static List<String> getHistoryTablesNameByTime(Date date, String prefix) {
    List<String> list = new ArrayList<>();
    String dateFm = DateUtil.dateToStr(date, DateConstants.DATE_FORMAT_YM);
    list.add(prefix + dateFm);
    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();;
    if (localDate.getDayOfMonth() == localDate.lengthOfMonth()) {
      //如果任务创建时间是当月最后一天,再关联查询数据发送历史时需要把下月的表一起加上
      String nextMonth = DateUtil.dateToStr(DateUtil.addDay(date, 1), DateConstants.DATE_FORMAT_YM);
      //防止时区出现错误,判断一下当前集合中是否已经存在下月表名
      if(!list.contains(nextMonth)) {
        //只有不存在时才需要添加下月表名
        list.add(prefix + nextMonth);
      }
    }
    return list;
  }


  /**
   * 检查json格式是否正确
   * @param config json
   */
  public static void checkJson(String config) {
    boolean json = true;
    //String failedMessage = "";
    try {
      JSONObject.parseObject(config, Object.class);
    }catch (Exception e) {
      e.printStackTrace();
      //failedMessage = e.getMessage();
      json = false;
    }
    if (!json) {
      //throw new ServiceException("配置json格式有误,原因:" + failedMessage);
      throw new ServiceException("配置json格式错误");
    }
  }

  public static String getPlanDayJson(PlanInfo planInfo, WeekEnum week) {
    String optionJson = "";
    if (1 == week.getInfo()) {
      optionJson = planInfo.getMonday();
    } else if (2 == week.getInfo()) {
      optionJson = planInfo.getTuesday();
    } else if (3 == week.getInfo()) {
      optionJson = planInfo.getWednesday();
    } else if (4 == week.getInfo()) {
      optionJson = planInfo.getThursday();
    } else if (5 == week.getInfo()) {
      optionJson = planInfo.getFriday();
    } else if (6 == week.getInfo()) {
      optionJson = planInfo.getSaturday();
    } else if (7 == week.getInfo()) {
      optionJson = planInfo.getSunday();
    }
    return optionJson;
  }

  public static List<String> getDataTables(String tablePrefix) {
    List<String> tables = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      tables.add(tablePrefix + (i+1));
    }
    return tables;
  }
}
